package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 
 * @author Sonny Sandberg
 */
public class Address 
{
    private int id;
    private String street;
    private ZipCode zipCode;
    private ArrayList<ActionListener> listeners;

    public Address(int id, String street, ZipCode zipCode)
    {
        this.id = id;
        this.street = street;
        this.zipCode = zipCode;
        listeners = new ArrayList();
        notifyListeners();
    }
    
    public Address()
    {
        zipCode = new ZipCode();
        listeners = new ArrayList();
        reset();
    }
    
    public void reset()
    {
        id = 0;
        street = "";
        zipCode.reset();
        notifyListeners();
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
        notifyListeners();
    }

    public String getStreet()
    {
        return street;
    }

    public void setStreet(String street)
    {
        this.street = street;
        notifyListeners();
    }

    public ZipCode getZipCode()
    {
        return zipCode;
    }

    public void setZipCode(ZipCode zipCode)
    {
        this.zipCode = zipCode;
        notifyListeners();
    }
    
    public void clearModel()
    {
        id = 0;
        street = "";
        zipCode.clearModel();
        notifyListeners();
    }
    
    public boolean isCompleted()
    {
        boolean answer = false;
        
        if (street.length() > 0 && zipCode.isCompleted() == true)
        {
            answer = true;
        }
        
        return answer;
    }
    
    @Override
    public String toString()
    {
        String text = "";
        
        text = String.format("ID: %d\n%s\n%s %s", id, street, zipCode.getZipCode(), zipCode.getCity());
        
        return text;
    }
    
    public void addListener(ActionListener listener) 
    {
        listeners.add(listener);
    }

    private void notifyListeners() 
    {
        ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Something changed");
        for (ActionListener listener : listeners) 
        {
            listener.actionPerformed(event);
        }
    }
}